package cn.itrip.search.service;

import cn.itrip.common.PageBean;
import cn.itrip.search.vo.HotelParam;
import cn.itrip.search.vo.ItripHoteVo;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SearchService {
    public List<ItripHoteVo> searchItripHotelListByHotCity(String cityId, Integer count) throws Exception{
        String url = "http://localhost:9080/solr/hotel";
        HttpSolrClient client = new HttpSolrClient(url);
        client.setParser(new XMLResponseParser());
        client.setConnectionTimeout(500);
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        query.addFacetQuery("cityId:"+cityId);
        query.setStart(0);
        query.setRows(count);
        return client.query(query).getBeans(ItripHoteVo.class);
    }

    public void searchItripHotelPage(HotelParam param,PageBean pageBean) throws Exception{

                String url = "http://localhost:9080/solr/hotel";
        HttpSolrClient client = new HttpSolrClient(url);
        client.setParser(new XMLResponseParser());
        client.setConnectionTimeout(500);
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        if(param.getKeywords() != null){
            query.addFacetQuery("keyword:"+param.getKeywords());
        }
        query.setStart((pageBean.getCurPage()-1)*pageBean.getPageSize());
        query.setRows(pageBean.getPageSize());
        QueryResponse response = client.query(query);
        pageBean.setTotal(new Long(response.getResults().getNumFound()).intValue());
        List<ItripHoteVo> list = client.query(query).getBeans(ItripHoteVo.class);
        pageBean.setRows(list);
    }
}
